﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using syc.Helper;
using System.Drawing;

namespace syc.Sale.Report
{
    public partial class 套管计划及发货量 : System.Web.UI.Page
    {
        protected string year;
        protected string month;
        protected string RiQiStart;
        protected string RiQiEnd;
        protected string RiQi;


        /// <summary>
        /// 读入所有的SESSION数据，向VIEW暴露数据项
        /// </summary>
        protected void read_session()
        {
            year = DList_year.SelectedValue;
            month = DList_month.SelectedValue;
        }

        private string sql = @"
SELECT 
    '丰原公司-' + dbo.产品订货单位.名称 AS 产品名称, 
    cp.品名, ht.日期, 
    ht.合同编号, 
    ht.数量 AS 订货量,
	(SELECT SUM(总重量) AS 出货量
	 FROM   dbo.产品出库单
	 WHERE (合同ID=ht.ID) AND
		   日期=ck.日期
	) AS 发货量, 
    ck.日期 AS 完成日期
FROM  
    dbo.产品 as cp INNER JOIN
    dbo.产品销售合同 as ht ON cp.ID = ht.产品ID INNER JOIN
    dbo.产品订货单位 ON dbo.产品订货单位.ID = ht.订货单位ID LEFT OUTER JOIN
    dbo.产品出库单 as ck ON ck.合同ID = ht.ID
WHERE 
    (cp.品名 = N'套管') AND
    (YEAR(ht.日期)=*年份*) AND
    (MONTH(ht.日期)=*月份*)
ORDER BY ck.日期";

        protected static DateTime date;

        //SQL的初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";

            if (!this.IsPostBack)
            {
                DList_year.SelectedValue = DateTime.Now.Year.ToString();
                DList_month.SelectedValue = DateTime.Now.Month.ToString();
                sql = sql.Replace("*年份*", DList_year.SelectedValue);
                sql = sql.Replace("*月份*", DList_month.SelectedValue);
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            read_session();
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
            //这一段分组的代码逻辑会强制执行，所以无需再在各个事件中重写
            GridViewHelper helper = new GridViewHelper(this.GridView1);
            helper.RegisterGroup("产品名称", true, true);
            helper.RegisterSummary("发货量", SummaryOperation.Sum, "产品名称");
            helper.RegisterSummary("发货量", SummaryOperation.Sum);
            helper.GroupHeader += new GroupEvent(helper_GroupHeader);
            helper.GroupSummary += new GroupEvent(helper_GroupSummary);
            helper.GeneralSummary += new FooterEvent(helper_GeneralSummary);
            helper.ApplyGroupSort();
        }

        //自定义分组标题样式
        private void helper_GroupHeader(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            row.Cells[0].Height = 33;
            row.BackColor = Color.LightGray;
            row.Cells[0].Text = "&nbsp;&nbsp;" + row.Cells[0].Text;

        }

        //自定义合计标题样式
        private void helper_GeneralSummary(GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.BackColor = Color.LightSlateGray;
            row.Cells[0].Text = "总计:";
            row.Height = 40;
        }


        //自定义合计标题样式
        private void helper_GroupSummary(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.Cells[0].Text = "小计:";
            row.Height = 40;
        }

        //年份变化时
        protected void DList_year_SelectedIndexChanged(object sender, EventArgs e)
        {
            sql = sql.Replace("*年份*", DList_year.SelectedValue);
            sql = sql.Replace("*月份*", DList_month.SelectedValue);
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        //月份变化时
        protected void DList_month_SelectedIndexChanged(object sender, EventArgs e)
        {
            sql = sql.Replace("*年份*", DList_year.SelectedValue);
            sql = sql.Replace("*月份*", DList_month.SelectedValue);
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }
    }
}
